<template>
    <div>
        <h1>科室添加</h1>
        <table class="table table-bordered">
           <tbody>
             <tr>
                <td align="right">科室名称</td>
                <td><input type="text" v-model="info.departmentName"></td>
            </tr>
            <tr>
                <td align="right">科室图片</td>
                <td>
                    <input type="file" @change="img">
                    <img :src="info.photo" v-if="info.photo!=''" while="50" height="50">
                </td>
            </tr>
            <tr>
                <td align="right">成立日期</td>
                <td><input type="date" v-model="info.startTime"></td>
            </tr>
            <tr>
                <td align="right"></td>
                <td><input type="button" value="提交" @click="add"></td>
            </tr>
           </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from "vue";
import axios from "axios";
import moment from "moment";
import {useRouter,useRoute} from "vue-router";
const router=useRouter();
const route=useRoute();
let info:any=reactive({
  "departmentName": "",
  "photo": "",
  "startTime": ""
})
const add=()=>
{
    if(info.departmentName=="")
    {
        alert("科室名称不能为空");
        return;
    }
    if(info.photo=="")
    {
        alert("科室图片不能为空");
        return;
    }
    if(info.startTime=="")
    {
        alert("成立日期不能为空");
        return;
    }
    axios({
        url:'http://localhost:5182/api/Money/AddDep',
        method:'post',
        data:info
    })
    .then((res)=>
    {
        console.log(res);
        if(res.data==-1)
        {
            alert("科室名称不唯一");
            return;
        }
        else if(res.data>0)
        {
            alert("添加成功");
            return;
        }
        else
        {
            alert("添加失败");
            return;
        }
    })
    .catch((err)=>
    {
        console.log(err);
    })
}
const img=(e:any)=>
{
    var obj=e.target.files[0];
    let f=new FormData();
    f.append("file",obj);
    axios({
        url:'http://localhost:5182/api/Money/Upload',
        method:'post',
        data:f
    })
    .then((res)=>
    {
        console.log(res);
        if(res.data=="不是图片")
        {
            alert("不是图片");
            return;
        }
        if(res.data=="大于2M")
        {
            alert("大于2M");
            return;
        }
       info.photo=res.data;
    })
    .catch((err)=>
    {
        console.log(err);
    })
}
</script>

<style scoped>

</style>